package base.Chapter00_Structures.C04_Stack;

/**
 * brackets check
 * Logic:
 * 左括号放入栈， 右括号与栈顶元素比较， 相同弹栈，不相同抛出异常
 */
public class C02_BracketsCheck {
    public static boolean check(String brackets) {
        boolean isRightful = false;
        if (brackets == null || brackets.length() <= 1) {
            return isRightful;
        }
        Stack2 stack = new Stack2();
        for (int i = 0; i < brackets.length(); i++) {
            char c = brackets.charAt(i);
            //c == '{' || c == '[' || c == '('
            //c == '}' || c == ']' || c == '0'
            if (c == '{' || c == '[' || c == '(')
                stack.push(c);
            else {
                char cPop = (char) stack.pop();

                switch (c) {
                    case ')':
                        if (cPop == '(')
                            isRightful = true;
                        break;
                    case ']':
                        if (cPop == '[')
                            isRightful = true;
                        break;
                    case '}':
                        if (cPop == '{')
                            isRightful = true;
                        break;
                    default:
                        break;
                }
            }
        }
        return isRightful;
    }
}
